<template>
  <div class="tinymce-box">
    <editor v-model="content" :init="init" :disabled="disabled" />
  </div>
</template>
<script>
import tinymce from 'tinymce/tinymce'
import Editor from '@tinymce/tinymce-vue'
import 'tinymce/themes/silver'
import 'tinymce/icons/default'
import 'tinymce/models/dom'
// 编辑器插件plugins
// 更多插件参考：https://www.tiny.cloud/docs/plugins/
import 'tinymce/plugins/image' // 插入上传图片插件
import 'tinymce/plugins/table' // 插入表格插件
import 'tinymce/plugins/lists' // 列表插件
import 'tinymce/plugins/wordcount' // 字数统计插件
import 'tinymce/plugins/code'
import 'tinymce/plugins/link'
import 'tinymce/plugins/pagebreak'
import 'tinymce/plugins/searchreplace'
import 'tinymce/plugins/preview'
import 'tinymce/skins/ui/oxide/skin.min.css'

export default {
  components: {
    editor: Editor
  },
  name: 'tinymce',
  props: {
    modelValue: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    plugins: {
      type: [String, Array],
      default: 'preview lists image table wordcount code link pagebreak searchreplace'
    },
    toolbar: {
      type: [String, Array],
      default:
        'undo redo | bold italic forecolor backcolor | fontfamily fontsize blocks | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists image table | removeformat'
    },
    imageUpload: {
      type: Function,
      default: function (blobInfo, progress) {
        const img = 'data:image/jpeg;base64,' + blobInfo.base64()
        console.log('图片上传处理：', img)
        progress(100)
      }
    }
  },
  data() {
    return {
      init: {
        language_url: './tinymce/langs/zh_CN.js',
        language: 'zh_CN',
        skin_url: './tinymce/skins/ui/oxide',
        content_css: './tinymce/skins/content/default/content.css',
        content_style: 'img {max-width:100%;}',
        plugins: this.plugins,
        toolbar: this.toolbar,
        branding: false,
        promotion: false,
        font_size_formats: '8pt 9pt 10pt 11pt 12pt 26pt 36pt',
        font_family_formats:
          '微软雅黑=Microsoft YaHei,Helvetica Neue,PingFang SC,sans-serif;苹果苹方=PingFang SC,Microsoft YaHei,sans-serif;宋体=simsun,serif;仿宋体=FangSong,serif;黑体=SimHei,sans-serif;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;',
        height: 580,
        width: '100%',
        autoresize_min_height: 580,
        autoresize_max_height: 1600,
        convert_urls: true,
        entity_encoding: 'raw',
        allow_script_urls: true,
        invalid_elements: 'script',
        images_upload_handler: this.imageUpload,
        images_reuse_filename: true,
        license_key: 'gpl'
      },
      content: this.modelValue
    }
  },
  mounted() {
    tinymce.init({})
  },
  methods: {},
  watch: {
    modelValue: {
      immediate: true,
      deep: true,
      handler(newValue) {
        this.content = newValue
      }
    },
    content(newValue) {
      this.$emit('update:modelValue', newValue)
    }
  }
}
</script>
<style>
.tinymce-box {
}

.tox-tinymce-aux {
  z-index: 13000000 !important;
}
</style>
